#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"


void LocalScene::OnInit()
{
	mCamera.SetPosition(glm::vec3(0.f, 0.04f, 7.f));
	mCamera.LookAt(glm::vec3(0.f, 0.04f, 7.f), glm::vec3(0.f, 0.04f, 0.f));
	mCamera.UpdateViewMatrix();

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");

	GeometryGenerator geometryGenerator;

#if 0

	GeometryGenerator::MeshData lightData = geometryGenerator.CreatePanel(1.f, 1.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* lightEntity = CreateEntity<LocalEntity>("light");
	lightEntity->AddComponent<Model>(lightData);
	// lightEntity->GetTransform().SetLocalPosition(glm::vec3(8.f, 8.f, 8.f));
	lightEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 3.f, 0.f));
	lightEntity->GetTransform().UpdateMatrices();
	lightEntity->SetColor(glm::vec4(1.f));

	lightEntity->mMaterial.emissive = glm::vec3(30.f);
	lightEntity->mMaterial.baseColor = glm::vec3(1.f);

#endif

#if 1

#if 1

	/*GeometryGenerator::MeshData bottomPanelData = geometryGenerator.CreatePanel(8.f, 6.f, glm::vec3(0.f, 1.f, 0.f));

	LocalEntity* bottomPanelEntity = CreateEntity<LocalEntity>("bottomPanel");
	bottomPanelEntity->AddComponent<Model>(bottomPanelData);
	bottomPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -2.f, 0.f));
	bottomPanelEntity->GetTransform().UpdateMatrices();
	bottomPanelEntity->SetColor(glm::vec4(0.90f, 0.72f, 0.88f, 1.f));

	bottomPanelEntity->mMaterial.baseColor = glm::vec3(0.90f, 0.72f, 0.88f);*/

	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.8f, 16, 16);
	// GeometryGenerator::MeshData sphereData = geometryGenerator.CreateGeosphere(0.8f, 2);

	LocalEntity* oneEntity = CreateEntity<LocalEntity>("one");
	oneEntity->AddComponent<Model>(sphereData);
	oneEntity->GetTransform().SetLocalPosition(glm::vec3(1.9f, -1.f, 0.f));
	oneEntity->GetTransform().SetLocalOrientation(glm::quat(glm::radians(glm::vec3(0.f, 0.f, 120.f))));
	oneEntity->GetTransform().UpdateMatrices();
	oneEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	oneEntity->mMaterial.baseColor = glm::vec3(1.0f, 0.765557f, 0.336057f);
	oneEntity->mMaterial.metallic = 0.3f;
	oneEntity->mMaterial.roughness = 0.f;

	LocalEntity* twoEntity = CreateEntity<LocalEntity>("two");
	twoEntity->AddComponent<Model>(sphereData);
	twoEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -1.f, 0.f));
	twoEntity->GetTransform().SetLocalOrientation(glm::quat(glm::radians(glm::vec3(0.f, 0.f, 120.f))));
	twoEntity->GetTransform().UpdateMatrices();
	twoEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	twoEntity->mMaterial.baseColor = glm::vec3(1.0f, 0.765557f, 0.336057f);
	twoEntity->mMaterial.metallic = 0.6f;
	twoEntity->mMaterial.roughness = 0.f;

	LocalEntity* threeEntity = CreateEntity<LocalEntity>("three");
	threeEntity->AddComponent<Model>(sphereData);
	threeEntity->GetTransform().SetLocalPosition(glm::vec3(-1.9f, -1.f, 0.f));
	threeEntity->GetTransform().SetLocalOrientation(glm::quat(glm::radians(glm::vec3(0.f, 0.f, 120.f))));
	threeEntity->GetTransform().UpdateMatrices();
	threeEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	threeEntity->mMaterial.baseColor = glm::vec3(1.0f, 0.765557f, 0.336057f);
	threeEntity->mMaterial.metallic = 0.9f;
	threeEntity->mMaterial.roughness = 0.f;

#else

	LocalEntity* torusknotEntity = CreateEntity<LocalEntity>("torusknot");
	torusknotEntity->AddComponent<Model>("Torusknot.obj");
	torusknotEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -1.f, 0.f));
	torusknotEntity->GetTransform().UpdateMatrices();
	torusknotEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	torusknotEntity->mMaterial.baseColor = glm::vec3(1.f, 1.f, 1.f);
	torusknotEntity->mMaterial.metallic = 0.6f;
	torusknotEntity->mMaterial.roughness = 0.1f;

#endif


#else

	GeometryGenerator::MeshData bottomPanelData = geometryGenerator.CreatePanel(8.f, 8.f, glm::vec3(0.f, 1.f, 0.f));

	LocalEntity* bottomPanelEntity = CreateEntity<LocalEntity>("bottomPanel");
	bottomPanelEntity->AddComponent<Model>(bottomPanelData);
	bottomPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -1.f, 0.f));
	bottomPanelEntity->GetTransform().UpdateMatrices();
	bottomPanelEntity->SetColor(glm::vec4(0.90f, 0.72f, 0.88f, 1.f));

	bottomPanelEntity->mMaterial.baseColor = glm::vec3(0.90f, 0.72f, 0.88f);


	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.3f, 12, 12);

	const uint32 GRID_DIM = 7;
	for (uint32 i = 0; i < GRID_DIM; ++i)
	{
		for (uint32 j = 0; j < GRID_DIM; ++j)
		{
			LocalEntity* sphereEntity = CreateEntity<LocalEntity>();
			sphereEntity->AddComponent<Model>(sphereData);
			sphereEntity->GetTransform().SetLocalPosition(glm::vec3(float(i - (GRID_DIM / 2.0f)) * 1.f + 1.f, 0.f, float(j - (GRID_DIM / 2.0f)) * 1.f) + 1.f);
			sphereEntity->GetTransform().UpdateMatrices();
			sphereEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

			sphereEntity->mMaterial.baseColor = glm::vec3(1.f, 0.f, 0.f);
		}
	}

#endif
}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	GameScene::OnImgui();

#if 0

	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);

	ImGui::Begin("MapOption");



	ImGui::End();

#endif
}
